import 'package:client_flutter_door/bean/cabinet.dart';
import 'package:client_flutter_door/model/door_model.dart';
import 'package:client_flutter_door/utils/info.dart';
import 'package:client_flutter_door/vars.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
// ignore: avoid_web_libraries_in_flutter
import 'dart:html' as html;
import 'package:provider/provider.dart';

class OpenDoor {
  final int BlId;
  final String DeviceId;
  final int mainboardno;
  final int lockno;
  OpenDoor(
      {required this.BlId,
      required this.DeviceId,
      required this.mainboardno,
      required this.lockno});
}

class Door {
  static List<Map<String, Object>> openDoorsNn = [
    {"BlId": 1, "DeviceId": "001", "mainboardno": 1, "lockno": 1},
    {"BlId": 2, "DeviceId": "001", "mainboardno": 1, "lockno": 2},
    {"BlId": 3, "DeviceId": "001", "mainboardno": 1, "lockno": 3},
    {"BlId": 4, "DeviceId": "001", "mainboardno": 1, "lockno": 4},
    {"BlId": 5, "DeviceId": "001", "mainboardno": 1, "lockno": 5},
    {"BlId": 6, "DeviceId": "001", "mainboardno": 1, "lockno": 6},
    {"BlId": 7, "DeviceId": "001", "mainboardno": 1, "lockno": 7},
    {"BlId": 8, "DeviceId": "001", "mainboardno": 1, "lockno": 8},
    {"BlId": 9, "DeviceId": "001", "mainboardno": 1, "lockno": 9},
    {"BlId": 10, "DeviceId": "001", "mainboardno": 1, "lockno": 10},
    {"BlId": 11, "DeviceId": "001", "mainboardno": 1, "lockno": 11},
    {"BlId": 12, "DeviceId": "001", "mainboardno": 1, "lockno": 12},
    {"BlId": 13, "DeviceId": "001", "mainboardno": 1, "lockno": 13},
    {"BlId": 14, "DeviceId": "001", "mainboardno": 1, "lockno": 14},
    {"BlId": 15, "DeviceId": "001", "mainboardno": 2, "lockno": 1},
    {"BlId": 16, "DeviceId": "001", "mainboardno": 2, "lockno": 2},
    {"BlId": 17, "DeviceId": "001", "mainboardno": 2, "lockno": 3},
    {"BlId": 18, "DeviceId": "001", "mainboardno": 2, "lockno": 4},
    {"BlId": 19, "DeviceId": "001", "mainboardno": 2, "lockno": 5},
    {"BlId": 20, "DeviceId": "001", "mainboardno": 2, "lockno": 6},
    {"BlId": 21, "DeviceId": "001", "mainboardno": 2, "lockno": 7},
    {"BlId": 22, "DeviceId": "001", "mainboardno": 2, "lockno": 8},
    {"BlId": 23, "DeviceId": "001", "mainboardno": 2, "lockno": 9},
    {"BlId": 24, "DeviceId": "001", "mainboardno": 2, "lockno": 10},
    {"BlId": 25, "DeviceId": "001", "mainboardno": 2, "lockno": 11},
    {"BlId": 26, "DeviceId": "001", "mainboardno": 2, "lockno": 12},
    {"BlId": 27, "DeviceId": "001", "mainboardno": 2, "lockno": 13},
    {"BlId": 28, "DeviceId": "001", "mainboardno": 2, "lockno": 14},
    {"BlId": 29, "DeviceId": "001", "mainboardno": 3, "lockno": 1},
    {"BlId": 30, "DeviceId": "001", "mainboardno": 3, "lockno": 2},
    {"BlId": 31, "DeviceId": "001", "mainboardno": 3, "lockno": 3},
    {"BlId": 32, "DeviceId": "001", "mainboardno": 3, "lockno": 4},
    {"BlId": 33, "DeviceId": "001", "mainboardno": 3, "lockno": 5},
    {"BlId": 34, "DeviceId": "001", "mainboardno": 3, "lockno": 6},
    {"BlId": 35, "DeviceId": "001", "mainboardno": 3, "lockno": 7},
    {"BlId": 36, "DeviceId": "001", "mainboardno": 3, "lockno": 8},
    {"BlId": 37, "DeviceId": "001", "mainboardno": 3, "lockno": 9},
    {"BlId": 38, "DeviceId": "001", "mainboardno": 3, "lockno": 10},
    {"BlId": 39, "DeviceId": "001", "mainboardno": 3, "lockno": 11},
    {"BlId": 40, "DeviceId": "001", "mainboardno": 3, "lockno": 12},
    {"BlId": 41, "DeviceId": "001", "mainboardno": 3, "lockno": 13},
    {"BlId": 42, "DeviceId": "001", "mainboardno": 3, "lockno": 14},
    {"BlId": 43, "DeviceId": "001", "mainboardno": 4, "lockno": 1},
    {"BlId": 44, "DeviceId": "001", "mainboardno": 4, "lockno": 2},
    {"BlId": 45, "DeviceId": "001", "mainboardno": 4, "lockno": 3},
    {"BlId": 46, "DeviceId": "001", "mainboardno": 4, "lockno": 4},
    {"BlId": 47, "DeviceId": "001", "mainboardno": 4, "lockno": 5},
    {"BlId": 48, "DeviceId": "001", "mainboardno": 4, "lockno": 6},
    {"BlId": 49, "DeviceId": "001", "mainboardno": 4, "lockno": 7},
    {"BlId": 50, "DeviceId": "001", "mainboardno": 4, "lockno": 8},
    {"BlId": 51, "DeviceId": "001", "mainboardno": 4, "lockno": 9},
    {"BlId": 52, "DeviceId": "001", "mainboardno": 4, "lockno": 10},
    {"BlId": 53, "DeviceId": "001", "mainboardno": 4, "lockno": 11},
    {"BlId": 54, "DeviceId": "001", "mainboardno": 4, "lockno": 12},
    {"BlId": 55, "DeviceId": "001", "mainboardno": 4, "lockno": 13},
    {"BlId": 56, "DeviceId": "001", "mainboardno": 4, "lockno": 14},
    {"BlId": 57, "DeviceId": "001", "mainboardno": 5, "lockno": 1},
    {"BlId": 58, "DeviceId": "001", "mainboardno": 5, "lockno": 2},
    {"BlId": 59, "DeviceId": "001", "mainboardno": 5, "lockno": 3},
    {"BlId": 60, "DeviceId": "001", "mainboardno": 5, "lockno": 4},
    {"BlId": 61, "DeviceId": "001", "mainboardno": 5, "lockno": 5},
    {"BlId": 62, "DeviceId": "001", "mainboardno": 5, "lockno": 6},
    {"BlId": 63, "DeviceId": "001", "mainboardno": 5, "lockno": 7},
    {"BlId": 64, "DeviceId": "001", "mainboardno": 5, "lockno": 8},
    {"BlId": 65, "DeviceId": "001", "mainboardno": 5, "lockno": 9},
    {"BlId": 66, "DeviceId": "001", "mainboardno": 5, "lockno": 10},
    {"BlId": 67, "DeviceId": "001", "mainboardno": 5, "lockno": 11},
    {"BlId": 68, "DeviceId": "001", "mainboardno": 5, "lockno": 12},
    {"BlId": 69, "DeviceId": "001", "mainboardno": 5, "lockno": 13},
    {"BlId": 70, "DeviceId": "001", "mainboardno": 5, "lockno": 14},
    {"BlId": 71, "DeviceId": "001", "mainboardno": 6, "lockno": 1},
    {"BlId": 72, "DeviceId": "001", "mainboardno": 6, "lockno": 2},
    {"BlId": 73, "DeviceId": "001", "mainboardno": 6, "lockno": 3},
    {"BlId": 74, "DeviceId": "001", "mainboardno": 6, "lockno": 4},
    {"BlId": 75, "DeviceId": "001", "mainboardno": 6, "lockno": 5},
    {"BlId": 76, "DeviceId": "001", "mainboardno": 6, "lockno": 6},
    {"BlId": 77, "DeviceId": "001", "mainboardno": 6, "lockno": 7},
    {"BlId": 78, "DeviceId": "001", "mainboardno": 6, "lockno": 8},
    {"BlId": 79, "DeviceId": "001", "mainboardno": 6, "lockno": 9},
    {"BlId": 80, "DeviceId": "001", "mainboardno": 6, "lockno": 10},
    {"BlId": 81, "DeviceId": "001", "mainboardno": 6, "lockno": 11},
    {"BlId": 82, "DeviceId": "001", "mainboardno": 6, "lockno": 12},
    {"BlId": 83, "DeviceId": "001", "mainboardno": 6, "lockno": 13},
    {"BlId": 84, "DeviceId": "001", "mainboardno": 6, "lockno": 14},
    {"BlId": 85, "DeviceId": "001", "mainboardno": 7, "lockno": 1},
    {"BlId": 86, "DeviceId": "001", "mainboardno": 7, "lockno": 2},
    {"BlId": 87, "DeviceId": "001", "mainboardno": 7, "lockno": 3},
    {"BlId": 88, "DeviceId": "001", "mainboardno": 7, "lockno": 4},
    {"BlId": 89, "DeviceId": "001", "mainboardno": 7, "lockno": 5},
    {"BlId": 90, "DeviceId": "001", "mainboardno": 7, "lockno": 6},
    {"BlId": 91, "DeviceId": "001", "mainboardno": 7, "lockno": 7},
    {"BlId": 92, "DeviceId": "001", "mainboardno": 7, "lockno": 8},
    {"BlId": 93, "DeviceId": "001", "mainboardno": 7, "lockno": 9},
    {"BlId": 94, "DeviceId": "001", "mainboardno": 7, "lockno": 10},
    {"BlId": 95, "DeviceId": "001", "mainboardno": 7, "lockno": 11},
    {"BlId": 96, "DeviceId": "001", "mainboardno": 7, "lockno": 12},
    {"BlId": 97, "DeviceId": "001", "mainboardno": 8, "lockno": 1},
    {"BlId": 98, "DeviceId": "001", "mainboardno": 8, "lockno": 2},
    {"BlId": 99, "DeviceId": "001", "mainboardno": 8, "lockno": 3},
    {"BlId": 100, "DeviceId": "001", "mainboardno": 8, "lockno": 4},
    {"BlId": 101, "DeviceId": "001", "mainboardno": 8, "lockno": 5},
    {"BlId": 102, "DeviceId": "001", "mainboardno": 8, "lockno": 6},
    {"BlId": 103, "DeviceId": "001", "mainboardno": 8, "lockno": 7},
    {"BlId": 104, "DeviceId": "001", "mainboardno": 8, "lockno": 8},
    {"BlId": 105, "DeviceId": "001", "mainboardno": 8, "lockno": 9},
    {"BlId": 106, "DeviceId": "001", "mainboardno": 8, "lockno": 10},
    {"BlId": 107, "DeviceId": "001", "mainboardno": 8, "lockno": 11},
    {"BlId": 108, "DeviceId": "001", "mainboardno": 8, "lockno": 12},
    {"BlId": 109, "DeviceId": "001", "mainboardno": 8, "lockno": 13},
    {"BlId": 110, "DeviceId": "001", "mainboardno": 8, "lockno": 14},
    {"BlId": 111, "DeviceId": "001", "mainboardno": 9, "lockno": 1},
    {"BlId": 112, "DeviceId": "001", "mainboardno": 9, "lockno": 2},
    {"BlId": 113, "DeviceId": "001", "mainboardno": 9, "lockno": 3},
    {"BlId": 114, "DeviceId": "001", "mainboardno": 9, "lockno": 4},
    {"BlId": 115, "DeviceId": "001", "mainboardno": 9, "lockno": 5},
    {"BlId": 116, "DeviceId": "001", "mainboardno": 9, "lockno": 6},
    {"BlId": 117, "DeviceId": "001", "mainboardno": 9, "lockno": 7},
    {"BlId": 118, "DeviceId": "001", "mainboardno": 9, "lockno": 8},
    {"BlId": 119, "DeviceId": "001", "mainboardno": 9, "lockno": 9},
    {"BlId": 120, "DeviceId": "001", "mainboardno": 9, "lockno": 10},
    {"BlId": 121, "DeviceId": "001", "mainboardno": 9, "lockno": 11},
    {"BlId": 122, "DeviceId": "001", "mainboardno": 9, "lockno": 12},
    {"BlId": 123, "DeviceId": "001", "mainboardno": 9, "lockno": 13},
    {"BlId": 124, "DeviceId": "001", "mainboardno": 9, "lockno": 14},
    {"BlId": 125, "DeviceId": "001", "mainboardno": 10, "lockno": 1},
    {"BlId": 126, "DeviceId": "001", "mainboardno": 10, "lockno": 2},
    {"BlId": 127, "DeviceId": "001", "mainboardno": 10, "lockno": 3},
    {"BlId": 128, "DeviceId": "001", "mainboardno": 10, "lockno": 4},
    {"BlId": 129, "DeviceId": "001", "mainboardno": 10, "lockno": 5},
    {"BlId": 130, "DeviceId": "001", "mainboardno": 10, "lockno": 6},
    {"BlId": 131, "DeviceId": "001", "mainboardno": 10, "lockno": 7},
    {"BlId": 132, "DeviceId": "001", "mainboardno": 10, "lockno": 8},
    {"BlId": 133, "DeviceId": "001", "mainboardno": 10, "lockno": 9},
    {"BlId": 134, "DeviceId": "001", "mainboardno": 10, "lockno": 10},
    {"BlId": 135, "DeviceId": "001", "mainboardno": 10, "lockno": 11},
    {"BlId": 136, "DeviceId": "001", "mainboardno": 10, "lockno": 12},
    {"BlId": 137, "DeviceId": "001", "mainboardno": 10, "lockno": 13},
    {"BlId": 138, "DeviceId": "001", "mainboardno": 10, "lockno": 14}
  ];
  static List<Map<String, Object>> openDoorsGl = [
    {"BlId": 1, "DeviceId": "001", "mainboardno": 1, "lockno": 1},
    {"BlId": 2, "DeviceId": "001", "mainboardno": 1, "lockno": 2},
    {"BlId": 3, "DeviceId": "001", "mainboardno": 1, "lockno": 3},
    {"BlId": 4, "DeviceId": "001", "mainboardno": 1, "lockno": 4},
    {"BlId": 5, "DeviceId": "001", "mainboardno": 1, "lockno": 5},
    {"BlId": 6, "DeviceId": "001", "mainboardno": 1, "lockno": 6},
    {"BlId": 7, "DeviceId": "001", "mainboardno": 1, "lockno": 7},
    {"BlId": 8, "DeviceId": "001", "mainboardno": 1, "lockno": 8},
    {"BlId": 9, "DeviceId": "001", "mainboardno": 1, "lockno": 9},
    {"BlId": 10, "DeviceId": "001", "mainboardno": 1, "lockno": 10},
    {"BlId": 11, "DeviceId": "001", "mainboardno": 1, "lockno": 11},
    {"BlId": 12, "DeviceId": "001", "mainboardno": 1, "lockno": 12},
    {"BlId": 13, "DeviceId": "001", "mainboardno": 1, "lockno": 13},
    {"BlId": 14, "DeviceId": "001", "mainboardno": 1, "lockno": 14},
    {"BlId": 15, "DeviceId": "001", "mainboardno": 2, "lockno": 1},
    {"BlId": 16, "DeviceId": "001", "mainboardno": 2, "lockno": 2},
    {"BlId": 17, "DeviceId": "001", "mainboardno": 2, "lockno": 3},
    {"BlId": 18, "DeviceId": "001", "mainboardno": 2, "lockno": 4},
    {"BlId": 19, "DeviceId": "001", "mainboardno": 2, "lockno": 5},
    {"BlId": 20, "DeviceId": "001", "mainboardno": 2, "lockno": 6},
    {"BlId": 21, "DeviceId": "001", "mainboardno": 2, "lockno": 7},
    {"BlId": 22, "DeviceId": "001", "mainboardno": 2, "lockno": 8},
    {"BlId": 23, "DeviceId": "001", "mainboardno": 2, "lockno": 9},
    {"BlId": 24, "DeviceId": "001", "mainboardno": 2, "lockno": 10},
    {"BlId": 25, "DeviceId": "001", "mainboardno": 2, "lockno": 11},
    {"BlId": 26, "DeviceId": "001", "mainboardno": 2, "lockno": 12},
    {"BlId": 27, "DeviceId": "001", "mainboardno": 3, "lockno": 1},
    {"BlId": 28, "DeviceId": "001", "mainboardno": 3, "lockno": 2},
    {"BlId": 29, "DeviceId": "001", "mainboardno": 3, "lockno": 3},
    {"BlId": 30, "DeviceId": "001", "mainboardno": 3, "lockno": 4},
    {"BlId": 31, "DeviceId": "001", "mainboardno": 3, "lockno": 5},
    {"BlId": 32, "DeviceId": "001", "mainboardno": 3, "lockno": 6},
    {"BlId": 33, "DeviceId": "001", "mainboardno": 3, "lockno": 7},
    {"BlId": 34, "DeviceId": "001", "mainboardno": 3, "lockno": 8},
    {"BlId": 35, "DeviceId": "001", "mainboardno": 3, "lockno": 9},
    {"BlId": 36, "DeviceId": "001", "mainboardno": 3, "lockno": 10},
    {"BlId": 37, "DeviceId": "001", "mainboardno": 3, "lockno": 11},
    {"BlId": 38, "DeviceId": "001", "mainboardno": 3, "lockno": 12},
    {"BlId": 39, "DeviceId": "001", "mainboardno": 4, "lockno": 1},
    {"BlId": 40, "DeviceId": "001", "mainboardno": 4, "lockno": 2},
    {"BlId": 41, "DeviceId": "001", "mainboardno": 5, "lockno": 1},
    {"BlId": 42, "DeviceId": "001", "mainboardno": 5, "lockno": 2},
    {"BlId": 43, "DeviceId": "001", "mainboardno": 5, "lockno": 3},
    {"BlId": 44, "DeviceId": "001", "mainboardno": 5, "lockno": 4},
    {"BlId": 45, "DeviceId": "001", "mainboardno": 5, "lockno": 5},
    {"BlId": 46, "DeviceId": "001", "mainboardno": 5, "lockno": 6},
    {"BlId": 47, "DeviceId": "001", "mainboardno": 5, "lockno": 7},
    {"BlId": 48, "DeviceId": "001", "mainboardno": 5, "lockno": 8},
    {"BlId": 49, "DeviceId": "001", "mainboardno": 5, "lockno": 9},
    {"BlId": 50, "DeviceId": "001", "mainboardno": 5, "lockno": 10},
    {"BlId": 51, "DeviceId": "001", "mainboardno": 5, "lockno": 11},
    {"BlId": 52, "DeviceId": "001", "mainboardno": 5, "lockno": 12},
    {"BlId": 53, "DeviceId": "001", "mainboardno": 6, "lockno": 1},
    {"BlId": 54, "DeviceId": "001", "mainboardno": 6, "lockno": 2},
  ];
  static void login() async {
    var headers = {'Content-Type': 'application/json'};
    var request =
        http.Request('POST', Uri.parse('${Vars.doorUrl}/Login/DoLogin'));
    request.body = json.encode(
        {"CuCode": "001", "UCode": "admin", "UPwd": "123", "imgcode1": "j33a"});
    request.headers.addAll(headers);

    http.StreamedResponse response = await request.send();

    if (response.statusCode == 200) {
      final pref = await Vars.prefes;
      var num = pref.getInt('waitingOpendoor');
      if (num != null || num != -1) {
        Info.snackbarWithoutContext('打开上次登录会话已过期未成功开门的柜门$num');
        pref.setInt("waitingOpendoor", -1);
        Future.delayed(const Duration(seconds: 3), () {
          Door.open(num!);
        });
      }
      print(await response.stream.bytesToString());
    } else {
      print(response.reasonPhrase);
    }
  }

  static Future<int> open(int num) async {
    var headers = {'Content-Type': 'application/json'};
    var request =
        http.Request('POST', Uri.parse('${Vars.doorUrl}/BoxsSet/OpenlockTest'));
    Cabinet? box =
        Vars.navigatorKey.currentContext!.read<DoorModel>().getBox(num);
    if (box == null) {
      Info.snackbarWithoutContext('没有匹配到$num的柜子');
      return -1;
    }
    // request.body = json.encode(openDoors[num - 1]);
    request.body = json.encode(box);
    request.headers.addAll(headers);
    http.StreamedResponse response = await request.send();

    if (response.statusCode == 200) {
      var result = await response.stream.bytesToString();
      if (result.contains('登录会话已过期')) {
        Info.snackbarWithoutContext('登录会话已过期');
        // 写入要打开的柜门号到waitingOpendoor
        final pref = await Vars.prefes;
        pref.setInt('waitingOpendoor', num);
        //重新加载页面获取cookie
        html.window.location.reload();
      }
      print(result);
    } else {
      print(response.reasonPhrase);
    }
    return response.statusCode;
  }
}
